package com.googlecode.jsimplemarket.wicket.components.grid;

import com.googlecode.jsimplemarket.wicket.pages.DefaultEditPage;
import java.io.Serializable;
import java.lang.reflect.InvocationTargetException;
import java.util.Arrays;
import org.apache.wicket.markup.html.panel.Panel;

/**
 *
 * @author Alexandru.Codescu
 */
public class PageParameters implements Serializable {

    private Class<? extends DefaultEditPage> editPageClass;
    private Class<? extends PopUpPanel> editPanelClass;
    private final Object[] constructorParameters;
    private final Class[] constructorParametersClass;
    
    public PageParameters() {
        this.constructorParameters = new Object[1];
        this.constructorParametersClass = new Class[1];
    }
    
    public PageParameters(Class<? extends DefaultEditPage> editPageClass, Object... constructorParameters) {
        this.editPageClass = editPageClass;
        this.constructorParameters = new Object[constructorParameters.length + 1];
        for (int i = 0; i < constructorParameters.length; i++) {
            Object object = constructorParameters[i];
            this.constructorParameters[i] = object;
        }
        
        this.constructorParametersClass = new Class[constructorParameters.length + 1];
        for (int i = 0; i < constructorParameters.length; i++) {
            Object object = constructorParameters[i];
            this.constructorParametersClass[i] = object.getClass();
        }
        
        this.constructorParametersClass[constructorParameters.length] = Long.class;
    }
    
    public void setId(Long id) {
        this.constructorParameters[this.constructorParameters.length - 1] = id;
    }

    public Class<? extends Panel> getEditPanelClass() {
        return editPanelClass;
    }
    
    public Class<? extends DefaultEditPage> getEditPageClass() {
        return editPageClass;
    }

    public Object[] getConstructorParameters() {
        return constructorParameters;
    }
    
    public DefaultEditPage getPageEdit() throws NoSuchMethodException, InstantiationException, IllegalAccessException, InvocationTargetException {
        if (editPageClass == null) {
            return null;
        }
        return editPageClass.getConstructor(constructorParametersClass).newInstance(constructorParameters);
    }
    
    public PopUpPanel getPanelEdit(String id) throws NoSuchMethodException, InstantiationException, IllegalAccessException, InvocationTargetException {
        if (editPanelClass == null) {
            return null;
        }
        Class[] paramsClass = Arrays.copyOf(constructorParametersClass, constructorParametersClass.length + 1);
        Object[] params = Arrays.copyOf(constructorParameters, constructorParameters.length + 1);
        paramsClass[constructorParametersClass.length] = String.class;
        params[constructorParameters.length] = id;
        return editPanelClass.getConstructor(paramsClass).newInstance(params);
    }
    
    public DefaultEditPage getPageInsert() throws NoSuchMethodException, InstantiationException, IllegalAccessException, InvocationTargetException {
        if (editPageClass == null) {
            return null;
        }
        return editPageClass.getConstructor(
                Arrays.copyOfRange(constructorParametersClass, 0, constructorParametersClass.length - 1)).newInstance(
                Arrays.copyOfRange(constructorParameters, 0, constructorParameters.length - 1));
    }
}
